package com.aaa.service.impl;

import com.aaa.dao.OrderDao;
import com.aaa.dto.AddOrderDto;
import com.aaa.dto.OrderDetailDto;
import com.aaa.dto.OrderDto;
import com.aaa.pojo.DefaultMsg;
import com.aaa.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderDao orderDao;

    @Override
    public List<OrderDto> findOrderByUserId(Integer userId) {
        return orderDao.findAllOrderByUserId(userId);
    }

    @Override
    public List<OrderDetailDto> findOrderDetailByOrderId(Integer orderId) {

        return orderDao.findOrderDetailByOrderId(orderId);
    }

    @Override
    public int updateStatusByOrderId(Integer orderId) {

        return orderDao.updateStatusByOrderId(orderId);
    }

    @Override
    public int deleteOrderByOrderId(Integer orderId) {

        int i1 = orderDao.deleteOrderByOrderId(orderId);
        int i2 = orderDao.deleteOrderDetailByOrderId(orderId);

        return i1 == 0 || i2 == 0 ? 0 : 1;
    }

    @Override
    public DefaultMsg updateStatusByOrderId_4(Integer orderId) {

        DefaultMsg defaultMsg = new DefaultMsg(200, 1, null, null);
        int i = orderDao.updateStatusByOrderId_4(orderId);
        if (i == 0) defaultMsg.setMsg(0);

        return defaultMsg;
    }

    @Override
    public DefaultMsg updateStatusByOrderId_1(Integer orderId) {

        DefaultMsg defaultMsg = new DefaultMsg(200, 1, null, null);
        int i = orderDao.updateStatusByOrderId_1(orderId);
        if (i == 0) defaultMsg.setMsg(0);

        return defaultMsg;
    }

    @Override
    public DefaultMsg addOrderByUserId(AddOrderDto addOrderDto) {

        DefaultMsg defaultMsg = new DefaultMsg(200, 1, null, null);
        int i1 = orderDao.addOrderByUserId(addOrderDto);
        System.out.println("----------------");
        System.out.println(addOrderDto);
        int i2 = orderDao.addOrderDetailByOrderId(addOrderDto);
        if (i1 == 0 || i2 == 0) defaultMsg.setMsg(0);
        return defaultMsg;
    }
}
